// 配置数据库信息
const usersql = require('../../mysql.js')
// 生成随机id
const setId = require('../../assets/id.js')

// 获取角色列表
exports.getRole = (req, res) => {
    const pagemsg = req.query
    const sql = 'select * from role'
    usersql.query(sql, (err, results) => {
        if (err) return res.cc(err)
        if (results.length !== 0) {
            var p = pagemsg.page * pagemsg.pagesize
            var arr = results.slice(p - pagemsg.pagesize, p)
            res.send({ status: 200, message: '获取列表成功', data: {rows:arr,total:results.length} })
        } else {
            res.cc('获取列表失败')
        }
    })
}

// 获取角色权限详情信息
exports.getRoleRightDetail = (req, res) => {
    const roleid = req.query.roleid
    const sql = 'select * from role_right where role_ids=?'
    usersql.query(sql, roleid,(err, results) => {
        if (err) return res.cc(err)
            var newarr=[]
        if (results.length !== 0) {
            results.forEach(val => {
                newarr.push(val.right_id)
            })
            res.send({ status: 200, message: '获取权限组成功', data: newarr })
        } else {
            res.send({ status: 200, message: '获取权限组成功', data: newarr })
        }
    })
}

// 获取角色权限详情信息
exports.getRoleRight = (req, res) => {
    const roleid = req.query.roleid
    const sql = 'select * from role,role_right where roleid=role_ids and roleid=?'
    usersql.query(sql, roleid,(err, results) => {
        if (err) return res.cc(err)
            var newdata = {
                roleid: results[0].roleid,
                name: results[0].name,
                details: results[0].details,
                permIds: []
            }        
        if (results.length !== 0) {

            results.forEach(val => {
                newdata.permIds.push(val.right_id)
            })
            res.send({ status: 200, message: '获取列表成功', data: newdata })
        } else {
            res.send({ status: 200, message: '获取列表成功', data: newdata })
        }
    })
}

// 获取角色详情信息
exports.getRoleDetail = (req, res) => {
    const roleid = req.query.roleid
    const sql = 'select * from role where roleid=?'
    usersql.query(sql, roleid,(err, results) => {
        if (err) return res.cc(err)
        if (results.length !== 0) {
            res.send({ status: 200, message: '获取列表成功', data: results[0] })
        } else {
            res.cc('获取列表失败')
        }
    })
}

// 给用户分配角色
exports.addUserRole = (req, res) => {
    // body包含角色role_id组合 用户user_id
    var user = req.body
    const sql = 'insert into user_role(id,user_id,role_id) values ?'
    const sql2 = 'delete from user_role where user_id=?'
    var arr = user.role_id.split('-')
    usersql.query(sql2, user.user_id, (err, results) => {
        if (err) return res.cc(err)
        if(arr.length>0){
            let newroles=[]
            arr.forEach(val=>{
                let itemid = setId(100000000,999999999)
                let item = [itemid,user.user_id,val]
                newroles.push(item)
            })
            usersql.query(sql, [newroles], (err, results) => {
                if (err) return res.cc(err)
                if (results.affectedRows === 0) {
                    res.cc('添加用户角色失败')
                }
                res.send({ status: 200, message: '更新用户权限成功'})
            }) 
        }
        
    })
}

// 添加角色
exports.addRole = (req, res) => {
    // 提交组织名和描述name details
    const user = req.body
    // user.roleid = setId(10000000,99999999)
    const sql = 'insert into role set ?'
    usersql.query(sql, user, (err, results) => {
        if (err) return res.cc(err)
        if (results.affectedRows !== 0) {
            res.send({ status: 200, message: '添加成功', data: user })
        } else {
            res.cc('添加失败')
        }
    })
}

// 编辑角色
exports.editRole = (req, res) => {
    // 提交组织名和描述name details
    const user = req.body
    const sql = 'update role set ? where roleid=?'
    usersql.query(sql, [user,user.roleid], (err, results) => {
        if (err) return res.cc(err)
        if (results.affectedRows !== 0) {
            res.send({ status: 200, message: '编辑成功', data: user })
        } else {
            res.cc('编辑失败')
        }
    })
}

// 删除角色
exports.deleteRole = (req, res) => {
    const roleid = req.query.roleid
    const sql = 'delete from role where roleid=? '
    usersql.query(sql, roleid, (err, results) => {
        if (err) return res.cc(err)
        if (results.affectedRows !== 0) {
            res.send({ status: 200, message: '删除成功' })
        } else {
            res.cc('删除失败')
        }
    })
}



// 编辑角色权限
exports.editRoleRight = (req, res) => {
    // 接收权限点字符串集合   right_id 用来接收添加权限   用-分割    right_id   role_ids
    const user = req.body
    var rightarr=[]
    if(user.right_id.length>0){
        rightarr = user.right_id.split('-')
    }
    const sql1 = 'insert into role_right(right_id,role_ids) values ?'
    const sql3 = 'delete from role_right where role_ids=?'
    usersql.query(sql3, user.role_ids, (err, results) => {
        if (err) return res.cc(err)
        let newrights = []
        rightarr.forEach(val => {
            let arr = [val, user.role_ids]
            newrights.push(arr)
        })
        usersql.query(sql1, [newrights], (err, results) => {
            if (err) return res.cc(err)
            if (results.affectedRows === 0) return res.cc('插入失败')
            res.send({ status: 200, message: '更新成功',data:[newrights.length,results.affectedRows]  })
        })
    })
}